package com.qf.fmall2202.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall2202.index.entity.Category;
import com.qf.fmall2202.index.dao.CategoryDao;
import com.qf.fmall2202.index.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.service.ProductImgService;
import com.qf.fmall2202.product.service.ProductService;
import com.qf.fmall2202.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author 徐英海
 * @since 2022-11-04
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductImgService productImgService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryCategoryList() {
        List<Category> categoryList = this.list();
        Map<Integer, List<Category>> categoryListMap = categoryList.stream()
                .collect(Collectors.groupingBy(Category::getParentId));
        List<Category> parentCategoryList = categoryListMap.get(0);
        findChildCategory(parentCategoryList, categoryListMap);
        /*categoryList = categoryList.stream()
                .filter(category -> category.getParentId() != 0)
                .collect(Collectors.toList());
        findChildCategory(parentCategoryList, categoryList);
        return ResultVo.ok("查询成功", parentCategoryList);*/
        return ResultVo.ok("查询成功", parentCategoryList);
    }

    private void findChildCategory(List<Category> parentCategoryList, Map<Integer, List<Category>> categoryListMap) {
        parentCategoryList.forEach(
                parent ->
                {
                    List<Category> childList = categoryListMap.get(parent.getCategoryId());
                    if (childList != null && childList.size() > 0) {
                        this.findChildCategory(childList, categoryListMap);
                    }
                    parent.setCategories(childList);
                }
        );
    }

    private void findChildCategory(List<Category> parentCategoryList, List<Category> categoryList) {
        parentCategoryList.forEach(
                parent -> {
                    List<Category> childList = categoryList.stream()
                            .filter(category -> category.getParentId().equals(parent.getCategoryId()))
                            .collect(Collectors.toList());
                    if (childList.size() > 0) {
                        this.findChildCategory(childList, categoryList);
                    }
                    parent.setCategories(childList);
                }
        );
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryCategoryRecommends() {
        List<Category> parentCategoryList = this.list(new QueryWrapper<Category>().eq("parent_id", 0));
        parentCategoryList.forEach(
                parent -> {
                    Page<Product> productPage = new Page<>(1, 6);
                    productPage.addOrder(OrderItem.desc("sold_num"));
                    productService.page(productPage, new LambdaQueryWrapper<Product>().eq(Product::getRootCategoryId, parent.getCategoryId()));
                    List<Product> productList = productPage.getRecords();
                    if (productList != null && productList.size() > 0) {
                        List<String> productIdList = productList.stream()
                                .map(Product::getProductId)
                                .collect(Collectors.toList());
                        List<ProductImg> productImgList = productImgService.list(
                                new QueryWrapper<ProductImg>()
                                        .in("item_id", productIdList)
                        );
                        Map<String, List<ProductImg>> map = productImgList.stream()
                                .collect(Collectors.groupingBy(ProductImg::getItemId));
                        productList.forEach(
                                product ->
                                {
                                    List<ProductImg> productImgs = map.get(product.getProductId());
                                    product.setImgs(productImgs == null ? new ArrayList<ProductImg>() : productImgs);
                                }
                        );
                    }
                    parent.setProducts(productList);
                }
        );
        return ResultVo.ok("查询成功", parentCategoryList);
    }
}
